﻿using DevExpress.XtraEditors;
using EFERP;
using HRM.DICTIONARY.DAL;
using System;
using System.Windows.Forms;

namespace HRM.DICTIONARY.UI
{
    public partial class xucBranchAdd : XtraForm
    {
        private hrm_branch _obj;
        private EmployeeLogicEx _logicEx;
        private readonly ICommon<hrm_branch> _exeDic;
        private CloseOrNew _closeNew = CloseOrNew.Close;

        public xucBranchAdd(ICommon<hrm_branch> exeDic)
        {
            InitializeComponent();
            this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Insert;
            this._obj = new hrm_branch();
            SetDataIntoUI();
        }

        public xucBranchAdd(hrm_branch currObj, ICommon<hrm_branch> exeDic)
        {
            InitializeComponent();
            this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Update;
            this._obj = currObj;
            SetDataIntoUI();
        }

        private void xucBranchAdd_FormClosing(object sender, FormClosingEventArgs e)
        {
            TotalClose();
        }

        private void SetDataIntoUI()
        {
            if (_obj == null)
                return;
            switch (_exeDic.myAction)
            {
                case Actions.Update:
                    txtMaChiNhanh.Text = _obj.branchcode;
                    txtTenChiNhanh.Text = _obj.branchname;
                    txtDiaChi.Text = _obj.address;
                    txtDienThoai.Text = _obj.phone;
                    txtNguoiDaiDien.Text = _obj.personname;
                    txtFax.Text = _obj.fax;
                    txtMoTa.Text = _obj.description;
                    cbbTongSLThuc.EditValue = _obj.factquantity;
                    cbbTongSLUocLuong.EditValue = _obj.quantity;
                    calLuongToiThieuVung.EditValue = _obj.minimumsalary;

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = false;
                    break;

                case Actions.Insert:
                    _obj = new hrm_branch();
                    txtMaChiNhanh.Text = _exeDic.NewID("CN");
                    txtTenChiNhanh.Text = "";
                    txtDiaChi.Text = "";
                    txtDienThoai.Text = "";
                    txtNguoiDaiDien.Text = "";
                    txtFax.Text = "";
                    txtMoTa.Text = "";
                    cbbTongSLThuc.EditValue = 0;
                    cbbTongSLUocLuong.EditValue = 0;
                    calLuongToiThieuVung.EditValue = 0;

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;

                case Actions.InsertMultiple:
                    _obj = new hrm_branch();
                    txtMaChiNhanh.Text = _exeDic.NewID("CN");
                    txtTenChiNhanh.Text = "";
                    txtDiaChi.Text = "";
                    txtDienThoai.Text = "";
                    txtNguoiDaiDien.Text = "";
                    txtFax.Text = "";
                    txtMoTa.Text = "";
                    cbbTongSLThuc.EditValue = 0;
                    cbbTongSLUocLuong.EditValue = 0;
                    calLuongToiThieuVung.EditValue = 0;

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
            }
        }

        private bool SetUIIntoData()
        {
            try
            {
                if (txtMaChiNhanh.Text == "" || txtTenChiNhanh.Text == "")
                {
                    MessageBox.Show(@"Vui lòng không để trống thông tin chi nhánh");
                    return false;
                }
                if (int.Parse(cbbTongSLThuc.EditValue.ToString())<=0)
                {
                    MessageBox.Show(@"Vui lòng nhập số lượng chứa tối đa nhân viên của Chi nhánh này.");
                    cbbTongSLThuc.Select();
                    return false;
                }
                _obj.branchcode = txtMaChiNhanh.Text;
                _obj.branchname = txtTenChiNhanh.Text;_obj.address = txtDiaChi.Text;
                _obj.phone = txtDienThoai.Text;
                _obj.personname = txtNguoiDaiDien.Text;
                _obj.fax = txtFax.Text;
                _obj.description = txtMoTa.Text;
                _obj.factquantity = int.Parse(cbbTongSLThuc.EditValue.ToString());
                _obj.quantity = int.Parse(cbbTongSLUocLuong.EditValue.ToString());
                _obj.minimumsalary = decimal.Parse(calLuongToiThieuVung.EditValue.ToString());

                _logicEx = new EmployeeLogicEx(_obj);
                bool kq = _logicEx.CheckQuantityInBranch();
                if (kq) return true;
                MessageBox.Show(_logicEx.ErrMsg);
                return false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return false;
        }

        private void TotalControl()
        {
            bool kq;
            switch (_exeDic.myAction)
            {
                case Actions.Insert:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(_obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Insert;
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;

                case Actions.Update:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.UpdateOnGridView(_obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Update;
                            this.Close();
                        }
                        else
                        {
                            _exeDic.ValueOfGridViewRow = null;
                            MessageBox.Show(@"Lỗi cập nhật dữ liệu vào hệ thống");
                        }
                    }
                    break;

                case Actions.InsertMultiple:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(_obj);
                        if (kq)
                        {
                            _exeDic.myAction = Actions.InsertMultiple;
                            SetDataIntoUI();
                            this.DialogResult = DialogResult.OK;
                            if (_closeNew == CloseOrNew.Close)
                                this.Close();
                            else
                                _closeNew = CloseOrNew.New;
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;
            }
        }

        private void TotalClose()
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            TotalClose();
        }

        private void btnLuuDong_Click(object sender, EventArgs e)
        {
            _closeNew = CloseOrNew.Close;
            TotalControl();
        }

        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            _exeDic.myAction = Actions.InsertMultiple;
            _closeNew = CloseOrNew.New;
            TotalControl();
        }
    }
}